//
//  FQDLargeArticleFeedCell.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLargeArticleFeedCell.h"
#import "FQDArticleCellBottomView.h"
#import "NSDate+FQDExtension.h"
#import "FQDRestModel.h"
#import "FQDArticleFeedCellItem.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface FQDLargeArticleFeedCell ()

@property (nonatomic, strong) UIImageView *articleImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) FQDArticleCellBottomView *articleBottomView;
@end

@implementation FQDLargeArticleFeedCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    UIImage *articleImage = [UIImage imageNamed:@"labFeedBgImage"];
    _articleImageView = [[UIImageView alloc] init];
    _articleImageView.image = articleImage;
    _articleImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_articleImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDBoldFontWithSize(16.0f);
    _titleLabel.textColor = FQDFeedTitleTextColor;
    _titleLabel.textAlignment = NSTextAlignmentNatural;
    _titleLabel.numberOfLines = 2;
    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.preferredMaxLayoutWidth = 300.0f;
    _titleLabel.backgroundColor = [UIColor whiteColor];
    _titleLabel.clipsToBounds = YES;
    [self.contentView addSubview:_titleLabel];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(14.0f);
    _descLabel.textColor = FQDFeedDetailTextColor;
    _descLabel.textAlignment = NSTextAlignmentNatural;
    _descLabel.numberOfLines = 3;
    _descLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    _descLabel.preferredMaxLayoutWidth = 300.0f;
    _descLabel.backgroundColor = [UIColor whiteColor];
    _descLabel.clipsToBounds = YES;
    [self.contentView addSubview:_descLabel];
    
    _articleBottomView = [[FQDArticleCellBottomView alloc] init];
    [self.contentView addSubview:_articleBottomView];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(weakSelf);
    }];
    
    [_articleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView);
        make.width.equalTo(weakSelf.contentView);
        make.height.equalTo(@200);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.articleImageView.mas_bottom).offset(15);
        make.left.equalTo(weakSelf.articleImageView.mas_left).offset(10);
        make.width.equalTo(weakSelf.contentView).offset(-20);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLabel.mas_bottom).offset(9);
        make.left.equalTo(weakSelf.articleImageView.mas_left).offset(10);
        make.bottom.equalTo(weakSelf.articleBottomView.mas_top).offset(-20);
        make.width.equalTo(weakSelf.titleLabel);
    }];
    
    [_articleBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLabel);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom).offset(-10);
        make.right.equalTo(weakSelf.descLabel);
    }];
}

- (void)configureCellWithCellItem:(FQDArticleFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDFeed *feed = cellItem.feed;
    
    FQDPost *post = feed.post;
    [self.articleImageView sd_setImageWithURL:[NSURL URLWithString:feed.image]];
    [self.titleLabel setText:post.title];
    [self.descLabel setText:post.desc];
    
    [self.articleBottomView.categoryLabel setText:post.category.title];
    [self.articleBottomView.commentButton setTitle:[NSString stringWithFormat:@"%@", @(post.comment_count)]
                                          forState:UIControlStateNormal];
    [self.articleBottomView.praiseButton setTitle:[NSString stringWithFormat:@"%@", @(post.praise_count)]
                                         forState:UIControlStateNormal];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:post.publish_time];
    [self.articleBottomView.dateLabel setText:[date dateStringSinceNow]];
}

#pragma mark Getters and Setters
+ (NSString *)reuseId
{
    return @"LargeArticleFeedCellId";
}

@end
